//
//  NetworkTools.m
//  Weibo-OC
//
//  Created by 焦林生 on 15/11/26.
//  Copyright © 2015年 cn.baidu. All rights reserved.
//

#import "NetworkTools.h"
@interface NetworkTools()
@property (nonatomic, copy) NSString *appKey;
@property (nonatomic, copy) NSString *appSecret;
@property (nonatomic, copy) NSString *redirectUrl;
@end
//网络工具协议
@protocol NetworkToolsProxy <NSObject>


@optional
/**
 *  网络请求方法
 *
 *  @param method     HTTP请求方法
 *  @param URLString  URLString
 *  @param parameters 参数字典
 *  @param success    成功回调
 *  @param failure    失败回调
 *
 *  @return NSURLSessionDataTask
 */
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                       URLString:(NSString *)URLString
                                      parameters:(id)parameters
                                         success:(void (^)(NSURLSessionDataTask *, id))success
                                         failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;

@end

/// 遵守网络协议 - 为了能够欺骗 Xcode 给一个智能提示，以及编译通过！
@interface NetworkTools() <NetworkToolsProxy>

@end


@implementation NetworkTools

// - 应用程序信息
-(NSString *)appKey {

    if (_appKey == nil) {
        _appKey = @"60773444";
    }
    return _appKey;
}

- (NSString *)appSecret {

    if (_appSecret == nil) {
        _appSecret = @"102c3d495872d421efad8c951893c3b2";
    }
    return _appSecret;
}

//OAuth重定向地址
- (NSString *)redirectUrl {

    if (_redirectUrl == nil) {
        _redirectUrl = @"http://www.baidu.com";
    }
    return _redirectUrl;
}

/// OAuth 授权 URL
- (NSURL *)OAuthURL {
    
    NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",self.appKey,self.redirectUrl];
    
    NSURL *url = [NSURL URLWithString:urlString];
    return url;
}

- (void)loadAccessToken:(NSString *)code finished:(RequestCallBack)finished {

    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    
    NSDictionary *params = @{@"client_id": self.appKey,
        @"client_secret": self.appSecret,
        @"grant_type": @"authorization_code",
        @"code": code,
        @"redirect_uri": self.redirectUrl
    };
    
    [self request:POST URLString:urlString parameters:params finished:finished];
}

+ (instancetype)sharedTools {

    static NetworkTools *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        //注意 末尾需要包含 '/'
        NSURL *baseURL = [NSURL URLWithString:@"http://httpbin.org/"];
        
        instance = [[self alloc] initWithBaseURL:baseURL];
        
        //设置反序列化格式
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    });
    
    return instance;
}

- (void)request:(RequestMethod)method URLString:(NSString *)URLString parameters:(id)parameters finished:(RequestCallBack)finished
{

    NSString *methodName = (method == GET) ? @"GET" : @"POST";
    
    //dataTaskWithHTTPMethod本类没有实现方法,但是父类实现了
    //在调用方法时, 如果本类没有提供,直接调用父类的方法,afn内部已经实现
    [self dataTaskWithHTTPMethod:methodName URLString:URLString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        finished(responseObject, nil);
        NSLog(@"%@",parameters);
        NSLog(@"123");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        finished(nil, error);
        NSLog(@"%@",error);
    }];

}


@end
